<%--
  Created by IntelliJ IDEA.
  User: 33904
  Date: 2025/1/8
  Time: 02:35
  To change this template use File | Settings | File Templates.
--%>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="Product.ProductDao" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>管理商品</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
        }
        .container {
            width: 80%;
            margin: auto;
            overflow: hidden;
            padding: 20px;
            background-color: #fff;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
        }
        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 20px;
        }
        form {
            background: #f9f9f9;
            padding: 20px;
            border-radius: 8px;
            margin-bottom: 20px;
            border: 1px solid #ddd;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input[type="text"],
        input[type="number"],
        textarea {
            width: 100%;
            padding: 10px;
            margin-bottom: 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }
        button {
            background: #007bff;
            color: #fff;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin-top: 10px;
        }
        button:hover {
            background: #0056b3;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            padding: 12px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }
        th {
            background-color: #007bff;
            color: #fff;
            font-weight: bold;
        }
        tr:hover {
            background-color: #f5f5f5;
        }
        .btn-delete {
            background: #dc3545;
            color: #fff;
            padding: 5px 10px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin-left: 10px;
        }
        .btn-delete:hover {
            background: #c82333;
        }
        .btn-edit {
            background: #28a745;
            color: #fff;
            padding: 5px 10px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        .btn-edit:hover {
            background: #218838;
        }
    </style>
</head>
<body>
<h1>管理商品</h1>
<%
    HashMap<String,String> user =  (HashMap<String, String>) request.getSession().getAttribute("user");
    if(user==null){
        request.getRequestDispatcher("login.jsp").forward(request,response);
        return;
    }
    int userId = Integer.parseInt(user.get("userId"));// 从会话中获取用户ID
%>
<!-- 增加商品的表单 -->
<form action="${pageContext.request.contextPath}/favorites" method="get">
    <input type="hidden" name="action" value="add">
    <input type="hidden" id="userId" name="userId" value="<%=userId%>">
    <label for="productName">商品名称:</label>
    <input type="text" id="productName" name="productName" required><br>
    <label for="productDescription">商品描述:</label>
    <textarea id="productDescription" name="productDescription" required></textarea><br>
    <label for="price">价格:</label>
    <input type="number" id="price" name="price" required><br>
    <label for="image">图片:</label>
    <input type="text" id="image" name="image" required><br>
    <label for="stockQuantity">库存数量:</label>
    <input type="number" id="stockQuantity" name="stockQuantity" required><br>
    <button type="submit">增加商品</button>
</form>

<!-- 商品列表 -->
<%
    ProductDao productDao = new ProductDao();
    ArrayList<HashMap<String, String>> productsList = productDao.connectToDatabase();
%>
<h2>商品列表</h2>
<table border="1">
    <tr>
        <th>商品ID</th>
        <th>商品名称</th>
        <th>商品描述</th>
        <th>价格</th>
        <th>图片</th>
        <th>库存数量</th>
        <th>操作</th>
    </tr>
    <% for (HashMap<String, String> product : productsList) { %>
    <tr>
        <td><%= product.get("pid") %></td>
        <td><%= product.get("name") %></td>
        <td><%= product.get("description") %></td>
        <td><%= product.get("price") %></td>
        <td><%= product.get("image") %></td>
        <td><%= product.get("total") %></td>

        <td>
            <form action="${pageContext.request.contextPath}/Product.ProductServlet" method="post">
                <input type="hidden" name="action" value="delete">
                <input type="hidden" name="productId" value="<%= product.get("pid") %>">
                <button type="submit">删除</button>
            </form>
        </td>
    </tr>
    <% } %>
</table>


</body>
</html>